;(function (window) {
	function Template(str) {
		this.str = str;
	}
	Template.prototype.format = function (o) {
		return this.str.replace(/\{(\w+)\}/g, function (a, b) {
			return o.hasOwnProperty(b) ? o[b] : a;
		});
	}

	var SKgiFrameUploadFileInnerHTML = ['<div style="display:none;">',
		'	<form action="{url}" method="post" id="SKgiFrameUploadFileId{id}" name="SKgiFrameUploadFileId" enctype="multipart/form-data">',
		'		<input type="hidden" name="callback" value="top.{callback}" />',
		'		<input type="file" name="{field}" accept="{accept}" />',
		'	</form>',
		'</div>'].join('');

	function iFrameUploadFile(url, field) {
		return new iFrameUploadFile.prototype.init(url, field);
	}

	iFrameUploadFile.prototype = {
		url : null,
		form : null,
		iFrame : null,
		id : 0,
		callback : '',
		field : '',
		accept:'image/png, image/jpg, image/jpeg, image/gif',
		init : function (url, field) {
			this.id = (new Date()).getTime();
			this.url = url;
			this.field = field;
			this.callback = 'SKgiFrameUploadFileCallback_' + this.id;
			this.createiFrame();
		},
		createiFrame : function () {
			this.iFrame = document.createElement('iframe');
			this.iFrame.width = 0;
			this.iFrame.height = 0;
			this.iFrame.style.display = 'none';
			var me = this;
			function iFrameOnload(){
				me.iFrame.contentWindow.document.write((new Template(SKgiFrameUploadFileInnerHTML)).format(me));
				me.form = me.iFrame.contentWindow.document.forms['SKgiFrameUploadFileId'];
				delete me;
			}
			if(-1 !== navigator.userAgent.indexOf('MSIE')){
				this.iFrame.attachEvent( 'onload', iFrameOnload);
			}else{
				this.iFrame.onload = iFrameOnload;
			}
			document.body.appendChild(this.iFrame);
			this.iFrame.src = 'about:blank';
		},
		browse : function (f) {
			if (typeof f === 'function') {
				var o = this.form[this.field];
				o.onchange = function () {
					f.call(o, o.value);
					delete o;
				}
			}
			this.form[this.field].click();
		},
		upload : function (fun) {
			var f = this.iFrame;
			window[this.callback] = function () {
				if (typeof fun === 'function') {
					fun.apply(window, arguments);
				}
				document.body.removeChild(f);
			}
			this.form.submit();
		},
		setAcceptFile : function (mime) {
			if(this.form === null){
				this.accept = mime;
			}else{
				this.form[this.field].setAttribute('accept', mime);
			}
		}
	};
	iFrameUploadFile.prototype.init.prototype = iFrameUploadFile.prototype;
	window.iFrameUploadFile = iFrameUploadFile;
})(window);
